package com.jiashihui.server1.validator;

import com.jiashihui.server1.annotation.Money;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
*describe: 
*
*@author jiashihui
*@date 2018/11/20
*/
public class MoneyValidator implements ConstraintValidator<Money, Double> {

    /**表示金额的正则表达式*/
    private String moneyReg = "^[0-9]+(.[0-9]{1,3})?$";
    private Pattern moneyPattern = Pattern.compile(moneyReg);

    @Override
    public void initialize(Money money) {

    }

    @Override
    public boolean isValid(Double value, ConstraintValidatorContext arg1) {
        if (value == null) {
            return true;
        }
        return moneyPattern.matcher(value.toString()).matches();
    }
}
